package cn.pku.ss.fa.ui.component;

import javax.naming.InitialContext;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zul.Window;

import cn.pku.ss.fa.bean.PhotoBean;
import cn.pku.ss.fa.sessionbean.AlbumManagerLocal;
import cn.pku.ss.fa.ui.Common;

/**
 * @author SirX
 */
public class PhotoWindowComponent extends Window {

	public PhotoWindowComponent() {
		Session session = Executions.getCurrent().getDesktop().getSession();
		try {
			//get photo id:
			String photoStringId = (String)session.getAttribute(Common.PHOTO_ID);
			this.photoId = Integer.parseInt( photoStringId.substring(5) );  //photoxx
			//get photo bean:
			this.photoBean = this.getPhotoBean();
			this.available = true;
		} catch (Exception e) {
			e.printStackTrace();
			this.available = false;
		}
		//get full width and height:
        try {
			this.photoFullWidth = "" + ((Integer) session.getAttribute(Common.DESKTOP_WIDTH) - 100) + "px";
			this.photoFullHeight = "" + ((Integer) session.getAttribute(Common.DESKTOP_HEIGHT + 110)) + "px";
		} catch (Exception e) {
			e.printStackTrace();
			this.photoFullWidth = "500px";
			this.photoFullHeight = "300px";
		}
	}
	
	public boolean isAvailable() {
		return available;
	}

	public int getPhotoId() {
		return this.photoId;
	}

	public String getPhotoName() {
		return this.photoBean.getName();
	}
	
	public String getPhotoDescription() {
		return this.photoBean.getDescription();
	}
	
	public String getPhotoURL() {
		return this.photoBean.getURL();
	}
	
	public String getPhotoFullWidth() {
		return this.photoFullWidth;
	}
	
	public String getPhotoFullHeight() {
		return this.photoFullHeight;
	}
	
	public void updatePhotoName(String newName) {
		try {
			InitialContext ctx = new InitialContext();
			AlbumManagerLocal albumDao = (AlbumManagerLocal)ctx.lookup("FriendsAnywhere/AlbumManager/local");
			albumDao.updatePhotoName(this.photoId, newName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updatePhotoDescription(String newDescription) {
		try {
			InitialContext ctx = new InitialContext();
			AlbumManagerLocal albumDao = (AlbumManagerLocal)ctx.lookup("FriendsAnywhere/AlbumManager/local");
			albumDao.updatePhotoDescription(this.photoId, newDescription);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//////////////////////////////////////////////////////////////////////////////

	private PhotoBean getPhotoBean() {
		try {
			InitialContext ctx = new InitialContext();
			AlbumManagerLocal albumDao = (AlbumManagerLocal)ctx.lookup("FriendsAnywhere/AlbumManager/local");
			return albumDao.getPhotoBean(this.photoId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
//		return this.getPhotoBean_test();
	}
	
//	private PhotoBean getPhotoBean_test() {
//		photoBean = new PhotoBean();
//		photoBean.setId(photoId);
//		photoBean.setName("Nanjing University");
//		photoBean.setURL("/user/0/album/0/0.jpg");
//		photoBean.setDescription("Nanjing Univertity many many words many many words");
//		return photoBean;
//	}
	
	//////////////////////////////////////////////////////////////////////////////

	private int photoId;
	private PhotoBean photoBean;
	private boolean available;

	private String photoFullWidth;
	private String photoFullHeight;

}